﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace BLL.Progect
{
    public class T_PUB_FILE
    {
        private readonly DALL.Progect.T_PUB_FILE dal = new DALL.Progect.T_PUB_FILE();


        public List<Model.Progect.T_PUB_FILE> GetFileList(string strID)
        {
            string strWhere = "";
            strWhere += string.Format(" and b.FILE_ID = '{0}' ", strID);
            return dal.GetFileList(strWhere);
        }

        /// <summary>
        /// 智能提示文件名称
        /// </summary>
        /// <param name="branch_id"></param>
        /// <returns></returns>
        public DataSet GetFileTitel(int branch_id)
        {
            return dal.GetFileTitel(branch_id);
        }

        public DataSet GetFileTitel()
        {
            return dal.GetFileTitel();
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <returns></returns>
        public void AddFileProject(Model.Progect.T_PUB_FILE FileModel, Model.Progect.T_PUB_FILEATTRIBUTE FileAttribute, IList<Model.Progect.T_PUB_PROJECTFILE> projectlist)
        {
            dal.AddFileProject(FileModel, FileAttribute, projectlist);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <returns></returns>
        public void AddFileProject(Model.Progect.T_PUB_FILE FileModel, Model.Progect.T_PUB_FILEATTRIBUTE FileAttribute)
        {
            dal.AddFileProject(FileModel, FileAttribute);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <returns></returns>
        public void EditFileProject(Model.Progect.T_PUB_FILE FileModel, Model.Progect.T_PUB_FILEATTRIBUTE FileAttribute, IList<Model.Progect.T_PUB_PROJECTFILE> addlist, IList<Model.Progect.T_PUB_PROJECTFILE> editlist)
        {
            dal.EditFileProject(FileModel, FileAttribute, addlist, editlist);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <returns></returns>
        public void EditFileProject(Model.Progect.T_PUB_FILE FileModel, Model.Progect.T_PUB_FILEATTRIBUTE FileAttribute)
        {
            dal.EditFileProject(FileModel, FileAttribute);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Model.Progect.T_PUB_FILE model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string FILE_ID)
        {
            return dal.Delete(FILE_ID);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Progect.T_PUB_FILE model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Progect.T_PUB_FILE GetModel(string FILE_ID)
        {

            return dal.GetModel(FILE_ID);
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere + " and ISUSED=1");
        }
        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Model.Progect.T_PUB_FILE> GetListArray(string strWhere)
        {
            return dal.GetListArray(strWhere);
        }
        /// <summary>
        /// 下载文件时对绑定文件数据
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetFileDownList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select A.FILE_ID,TYPE,FILE_NAME,FILE_TITEL,UPTIME,USERNAME,ISFLAG,BRANCH_NAME,PROJECTREAMRK,URLThumbnail,URLOriginal,URLVideoPhoto,A.BRANCH_ID,B.MATERIAL_STAR,B.PROJECT_TYPE ");
            strSql.Append(" from T_PUB_FILE as A ,T_PUB_BRANCH as D,T_PUB_FILEATTRIBUTE as B  ");
            strSql.Append("where UPTIME in(select Convert(nvarchar,min(UPTIME),121) from T_PUB_FILE WHERE  SUBstring(FILE_ID,1,36) in (select distinct(SUBSTRING(FILE_ID,1,36)) from T_PUB_FILE)group by SUBSTRING(FILE_ID,1,36))");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 文件同步
        /// </summary>
        /// <param name="fileguid"></param>
        /// <param name="isfileattr"></param>
        /// <param name="isfileproject"></param>
        /// <param name="branchfrom"></param>
        /// <param name="branchto"></param>
        /// <returns></returns>
        public bool Synchronizaion(string fileguid, bool isfileattr, bool isfileproject, int branchfrom, int branchto)
        {
            //string[] ParaStr = new string[] { fileguid, isfileattr.ToString(), isfileattr.ToString(), branchfrom.ToString(), branchto.ToString() };
            Database db = DatabaseFactory.CreateDatabase();
            try
            {
                db.ExecuteDataSet("DeleteFileToAllBranch", fileguid, isfileattr, isfileproject, branchfrom, branchto);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }


        /// <summary>
        /// 同步文件到所有分店
        /// </summary>
        /// <param name="fileguid"></param>
        /// <param name="isfileattr"></param>
        /// <param name="isfileproject"></param>
        /// <param name="branchfrom"></param>
        /// <returns></returns>
        public bool SynchronizaionToAllBranch(string fileguid, bool isfileattr, bool isfileproject, int branchfrom)
        {
            //string[] ParaStr = new string[] { fileguid, isfileattr.ToString(), isfileattr.ToString(), branchfrom.ToString(), branchto.ToString() };
            Database db = DatabaseFactory.CreateDatabase();
            try
            {
                db.ExecuteDataSet("SynchronizationFileToAllBranch", fileguid, isfileattr, isfileproject, branchfrom);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }


        /// <summary>
        /// 同步主题婚礼
        /// </summary>
        /// <param name="fileguid"></param>
        /// <param name="branchfrom"></param>
        /// <returns></returns>
        public bool SynchronizaionThemeToAllBranch(string fileguid, int branchfrom)
        {
            //string[] ParaStr = new string[] { fileguid, isfileattr.ToString(), isfileattr.ToString(), branchfrom.ToString(), branchto.ToString() };
            Database db = DatabaseFactory.CreateDatabase();
            try
            {
                db.ExecuteDataSet("SynchronizationThemeToAllBranch", fileguid, branchfrom);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        public bool DeleteFile(string fileguid, int user_id, string user_name)
        {
            //string[] ParaStr = new string[] { fileguid, isfileattr.ToString(), isfileattr.ToString(), branchfrom.ToString(), branchto.ToString() };
            Database db = DatabaseFactory.CreateDatabase();
            try
            {
                db.ExecuteDataSet("DeleteFileToAllBranch", fileguid, user_id, user_name);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Model.Progect.T_PUB_FILE> GetProjectFile(string strProjectID)
        {
            string strWhere = string.Format(" FILE_ID in (select FILE_ID from T_PUB_PROJECTFILE where PROJECT_ID='{0}' and ISUSED=1 )", strProjectID);
            return dal.GetListArray(strWhere);
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetFileList(string titel, int state, DateTime begintime, DateTime endtime, int branch_id, string type, string showmodel)
        {
            string strWhere = "ISUSED = 1 ";
            if (!string.IsNullOrEmpty(titel))
            {
                strWhere += string.Format(" and FILE_TITEL like '%{0}%'", titel);
            }
            if (state != -1)
            {
                strWhere += string.Format(" and ISFLAG = '{0}'", state);
            }
            if (!string.IsNullOrEmpty(begintime.ToString()))
            {
                strWhere += string.Format(" and UPTIME >= '{0}' ", begintime);
            }
            if (!string.IsNullOrEmpty(endtime.ToString()))
            {
                strWhere += string.Format(" and UPTIME <= '{0}' ", endtime);
            }
            if (!string.IsNullOrEmpty(branch_id.ToString()))
            {
                strWhere += string.Format(" and B.BRANCH_ID = '{0}' ", branch_id);
            }
            if (type != "-1")
            {
                strWhere += string.Format(" and TYPE = '{0}' ", type);
            }
            if (showmodel != "-1")
            {
                strWhere += string.Format(" and PROJECTREAMRK = '{0}' ", showmodel);
            }
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetFileList(string titel, int state, int branch_id, string type)
        {
            string strWhere = "ISUSED = 1";
            if (!string.IsNullOrEmpty(titel))
            {
                strWhere += string.Format(" and FILE_TITEL like '%{0}%'", titel);
            }
            if (state != -1)
            {
                strWhere += string.Format(" and ISFLAG = '{0}'", state);
            }
            if (!string.IsNullOrEmpty(branch_id.ToString()))
            {
                strWhere += string.Format(" and B.BRANCH_ID = '{0}' ", branch_id);
            }
            if (type != "-1")
            {
                strWhere += string.Format(" and TYPE = '{0}' ", type);
            }
            return dal.GetList(strWhere);
        }
    }
}
